﻿
var alertFun = window.alert;

window.alert = function (str) {

    try {

        if (str.indexOf("www.miniui.com") == -1) {

            alertFun(str);

        }
        else {

        }

    } catch (Exception) {

        //alertFun(str);

    }

};


//用户状态（是否冻结）
var Valids = [{ "id": "0", "text": "0→启用" }, { "id": "1", "text": "1→禁用"}];
//回收站标识
var Recycleds = [{ "id": "0", "text": "0→有效" }, { "id": "1", "text": "1→无效"}];
//性别
var Genders = [{ "id": "0", "text": "女" }, { "id": "1", "text": "男"}];

function onValidRenderer(e) {
    for (var i = 0, l = Valids.length; i < l; i++) {
        var g = Valids[i];
        if (g.id == e.value && g.id == "1") return "<a style=\"color=Red\">" + g.text + "</a>";
        else if (g.id == e.value)
            return g.text;
    }
    return "";
}

function onRecycledRenderer(e) {
    for (var i = 0, l = Recycleds.length; i < l; i++) {
        var g = Recycleds[i];
        if (g.id == e.value && g.id == "1") return "<a style=\"color=Red\">" + g.text + "</a>";
        else if (g.id == e.value)
            return "<a style=\"color=Blue\">" + g.text + "</a>";
    }
    return "";
}

function onGenderRenderer(e) {
    for (var i = 0, l = Genders.length; i < l; i++) {
        var g = Genders[i];
        if (g.id == e.value && g.id == "0") return "<a style=\"color=Red\">" + g.text + "</a>";
        else if (g.id == e.value)
            return g.text;
    }
    return "";
}

function onSelectUser(e) {
    var btnEdit = this;
    mini.open({
        url: "/Pub/PubSelect/SelectUser.aspx",
        showMaxButton: false,
        title: "人员选择",
        width: 800,
        height: 400,
        onload: function () {
            //打开子页面时传递参数
            var iframe = this.getIFrameEl();
            var data = { MultiSelect: "0" };
            iframe.contentWindow.SetPara(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                if (data) {
                    var userText = "";
                    var userValue = "";
                    for (var i = 0; i < data.length; i++) {
                        userText = data[i].user_name_ + "," + userText;
                        userValue = data[i].user_id_ + "," + userValue;
                    }
                    //去除最后一个,
                    userText = userText.substr(0, userText.length - 1);
                    userValue = userValue.substr(0, userValue.length - 1);
                    btnEdit.setValue(userValue);
                    btnEdit.setText(userText);
                }
            }
            if (action == "clear") {
                btnEdit.setValue("");
                btnEdit.setText("");
            }

        }
    });
}

function onSelectUserMul(e) {
    var btnEdit = this;
    var val = mini.get(this.id).getValue();
    var text = mini.get(this.id).getText();
    mini.open({
        url: "/Pub/PubSelect/SelectUserMul.aspx",
        showMaxButton: false,
        title: "人员选择",
        width: 800,
        height: 435,
        onload: function () {
            //打开子页面时传递参数
            var iframe = this.getIFrameEl();
            var data = { selectValues: val, selectTexts: text, type: 1 };
            iframe.contentWindow.SetPara(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                // data = mini.clone(data);//无效
                if (data) {
                    var userText = "";
                    var userValue = "";
                    userText = data.user_name_;
                    userValue = data.user_id_;

                    //去除最后一个,
                    //                    userText = userText.substr(0, userText.length - 1);
                    //                    userValue = userValue.substr(0, userValue.length - 1);
                    //                    if (val != '') {
                    //                        userValue = val + "," + userValue;
                    //                    }
                    //                    if (text != '') {
                    //                        userText = text + "," + userText;
                    //   }

                    btnEdit.setValue(userValue);
                    btnEdit.setText(userText);
                }
            }
            if (action == "clear") {
                btnEdit.setValue("");
                btnEdit.setText("");
            }

        }
    });
}
function onSelectUserByLoginName(e) {
    var btnEdit = this;
    mini.open({
        url: "/Pub/PubSelect/SelectUser.aspx",
        showMaxButton: false,
        title: "人员选择",
        width: 800,
        height: 400,
        onload: function () {
            //打开子页面时传递参数
            var iframe = this.getIFrameEl();
            var data = { MultiSelect: "0" };
            iframe.contentWindow.SetPara(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                if (data) {
                    var userText = "";
                    var userValue = "";
                    for (var i = 0; i < data.length; i++) {
                        userText = data[i].user_name_ + "," + userText;
                        userValue = data[i].login_name_ + "," + userValue;
                    }
                    //去除最后一个,
                    userText = userText.substr(0, userText.length - 1);
                    userValue = userValue.substr(0, userValue.length - 1);
                    btnEdit.setValue(userValue);
                    btnEdit.setText(userText);
                }
            }
            if (action == "clear") {
                btnEdit.setValue("");
                btnEdit.setText("");
            }

        }
    });
}
function onSelectUserMulByLoginName(e) {
    var btnEdit = this;
    var val = mini.get(this.id).getValue();
    var text = mini.get(this.id).getText();
    mini.open({
        url: "/Pub/PubSelect/SelectUserMul.aspx",
        showMaxButton: false,
        title: "人员选择",
        width: 800,
        height: 435,
        onload: function () {
            //打开子页面时传递参数
            var iframe = this.getIFrameEl();
            var data = { selectValues: val, selectTexts: text, type: 0 };
            iframe.contentWindow.SetPara(data);
        },
        ondestroy: function (action) {
            //会议参与人员
            var obj;
            var Id = btnEdit.getId();
            if (Id == "meeting_join_man")
                obj = $("#spMeetingJoinName");

            //纪要人员
            else if (Id == "summary_man" || Id == "stage_join_man")
                obj = $("#spSummaryMan");
            //推荐人员
            else if (id = "recommended_man")
                obj = $("#spRecommendedMan");

            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                // data = mini.clone(data);//无效
                if (data) {
                    var userText = "";
                    var userValue = "";
                    userText = data.user_name_.replace(/,/g, "、");   //替换逗号,为顿号、
                    userValue = data.login_name_;
                    btnEdit.setValue(userValue);
                    btnEdit.setText(userText);
                    if (obj != null) {
                        var userTextList = userText.split("、");
                        //人员超过25人则全部显示
                        if (id == "recommended_man") {                       
                            //                            userText = obj.html() + "、" + userText;
                            //                            obj.html(userText);
                        } else {
                            if (userTextList.length > 25)
                                obj.html(userText);
                            else
                                obj.html("");
                        }
                    }
                }
            }
            if (action == "clear") {
                btnEdit.setValue("");
                btnEdit.setText("");
                if (obj != null)
                    obj.html("");
            }
        }
    });
}
/*
function onSelectUserMul(e) {
var btnEdit = this;
//    var val = mini.get(this.id).getValue();
//    var text = mini.get(this.id).getText();
mini.open({
url: "/Pub/PubSelect/SelectUser.aspx",
showMaxButton: false,
title: "人员选择",
width: 800,
height: 400,
onload: function () {
//打开子页面时传递参数
var iframe = this.getIFrameEl();
var data = { MultiSelect: "1"}//, selectVlues: val, selectTexts: text };
iframe.contentWindow.SetPara(data);
},
ondestroy: function (action) {
if (action == "ok") {
var iframe = this.getIFrameEl();
var data = iframe.contentWindow.GetData();
data = mini.clone(data);
if (data) {
var userText = "";
var userValue = "";
for (var i = 0; i < data.length; i++) {
userText = data[i].user_name_ + "," + userText;
userValue = data[i].user_id_ + "," + userValue;
}
//去除最后一个,
userText = userText.substr(0, userText.length - 1);
userValue = userValue.substr(0, userValue.length - 1);
//                    if (val != '') {
//                        userValue = val + "," + userValue;
//                    }
//                    if (text != '') {
//                        userText = text + "," + userText;
//   }

btnEdit.setValue(userValue);
btnEdit.setText(userText);
}
}
if (action == "clear") {
btnEdit.setValue("");
btnEdit.setText("");
}

}
});
}*/

function onSelectComp(e) {
    var btnEdit = this;

    mini.open(
        {
            url: "/Pub/PubSelect/SelectGroupTree.aspx",
            showMaxButton: false,
            title: "选择公司/部门",
            width: 350,
            height: 450,
            onload: function () {
                var iframe = this.getIFrameEl();
                iframe.contentWindow.SetDefaultSelect({ group_id_: btnEdit.getValue() });
            },
            ondestroy: function (action) {
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.GetData();
                    data = mini.clone(data);
                    if (data) {
                        btnEdit.setValue(data.group_id_);
                        btnEdit.setText(data.group_name_);
                    }
                }
                if (action == "clear") {
                    btnEdit.setValue("");
                    btnEdit.setText("");
                }
            }
        });
}

function onSelectTaskType(e) {
    var btnEdit = this;
    mini.open({
        url: "/Pub/PubSelect/SelectTaskType.aspx?",
        showMaxButton: false,
        title: "选择任务类别",
        width: 300,
        height: 450,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { option_: "0", recycled_: "0" };
            iframe.contentWindow.SetPara(data);
            iframe.contentWindow.SetDefaultSelect(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                if (data) {
                    btnEdit.setValue(data.category_id_);
                    btnEdit.setText(data.name_);
                }
            }
            if (action == "clear") {
                btnEdit.setValue("");
                btnEdit.setText("");
            }
        }
    });
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

function InitTaskProperty(taskID, controlID) {
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetTaskPropertyWithName",
        cache: false,
        data: { task_id_: taskID },
        success: function (text) {
            var o = mini.decode(text);
            mini.get(controlID).setValue(o.ids);
            mini.get(controlID).setText(o.names);
        }
    });
}

function InitTaskCategory(categoryID, controlID) {
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetTaskTypeList",
        cache: false,
        data: { category_id_: categoryID },
        success: function (text) {
            var o = mini.decode(text);
            mini.get(controlID).setValue(o[0].category_id_);
            mini.get(controlID).setText(o[0].name_);
        }
    });
}

function InitTaskCc(taskID, controlID) {
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetTaskUserList",
        cache: false,
        data: { task_id_: taskID },
        success: function (text) {
            var o = mini.decode(text);
            var ids = '', names = '';
            for (var i = 0; i < o.data.length; i++) {
                ids += o.data[i].task_user_id_ + ',';
                names += o.data[i].task_user_name_ + ',';
            }
            mini.get(controlID).setValue(ids.substring(0, ids.length - 1));
            mini.get(controlID).setText(names.substring(0, names.length - 1));
        }
    });
}

function AppendTimeLineHtml(taskID, index, year, currentWeek, startTime, endTime, type, estimatedStartTime) {
    if (!CompareDate(estimatedStartTime, endTime)) return '';
    //startTime = new Date(startTime);
    //endTime = new Date(endTime).Format("MM.dd");
    var layer = '<table style="background-color: #F0F0F0; margin-top: 5px; width: 100%"><tr><td class="tdleft">汇报区间：</td><td style="width:300px">' + year + '年' + currentWeek;
    layer += type == 0 ? '(' + startTime.Format("MM.dd") + '~' + endTime.Format("MM.dd") + ')' : '';
    layer += '</td><td class="tdleft">工作目标：</td><td>';
    layer += '<input id="type_' + index + '" class="mini-textbox" style="display:none" value="' + type + '" />';
    layer += '<input id="start_time_' + index + '" class="mini-textbox" style="display:none" value="' + startTime.Format("yyyy-MM-dd") + '" />';
    layer += '<input id="end_time_' + index + '" class="mini-textbox" style="display:none" value="' + endTime.Format("yyyy-MM-dd") + '" />';
    layer += '<input id="target_' + index + '" class="mini-textbox" style="width: 99%" /></td></tr>';
    layer += '<tr><td class="tdleft">汇报内容：</td><td colspan="3"><input id="content_' + index + '" style="width: 99%; height: 80px" class="mini-textarea" /></td></tr></table>';
    return layer;
}

function GetWeekOfYear(a, b, c) {
    var d1 = new Date(a, b - 1, c);
    var d2 = new Date(a, 0, 1);
    var d = Math.round((d1 - d2) / 24 / 60 / 60 / 1000);
    return Math.ceil((d + ((d2.getDay() + 1) - 1)) / 7);
};

function GetWeekOfMonth(a, b, c) {
    /* 
    a = d = 当前日期 
    b = 6 - w = 当前周的还有几天过完（不算今天） 
    a + b 的和在除以7 就是当天是当前月份的第几周 
    */
    var date = new Date(a, parseInt(b) - 1, c), w = date.getDay() == 0 ? 7 : date.getDay(), d = date.getDate();
    return Math.ceil((d + 6 - w) / 7);
}

function GetWeekByDate(startDate, endDate) {
    var startMonth = (new Date(startDate)).Format("MM");
    var endMonth = (new Date(endDate)).Format("MM");
    if (startMonth == endMonth) {
        return [GetWeekOfYear((new Date(startDate)).Format("yyyy"), (new Date(startDate)).Format("MM"), (new Date(startDate)).Format("dd")) + "周", 0];
    } else {
        if (startMonth == "03") {
            return ["一季度", 2];
        }
        else if (startMonth == "06") {
            return ["二季度", 2];
        }
        else if (startMonth == "09") {
            return ["三季度", 2];
        }
        else if (startMonth == "12") {
            return ["四季度", 2];
        } else
            return [(new Date(startDate)).Format("MM") + "月", 1];
    }
}
function GetDifWeek(littleDate, bigDate) {
    var dlittle = new Date((new Date(littleDate)).getYear(), (new Date(littleDate)).getMonth() - 1, (new Date(littleDate)).Format("dd"));
    var dbig = new Date((new Date(bigDate)).getYear(), (new Date(bigDate)).getMonth() - 1, (new Date(bigDate)).Format("dd"));
    var d = Math.round((dbig - dlittle) / 24 / 60 / 60 / 1000);
    return Math.ceil((d + (((new Date(dlittle)).getDay() + 1) - 1)) / 7);
}
//比较日期大小，如果date
function CompareDate(littleDate, bigDate) {
    var d1Year = new Date(littleDate).Format("yyyy");
    var d1Month = new Date(littleDate).Format("MM");
    var d1Day = new Date(littleDate).Format("dd");
    var d2Year = new Date(bigDate).Format("yyyy");
    var d2Month = new Date(bigDate).Format("MM");
    var d2Day = new Date(bigDate).Format("dd");
    if (d2Year > d1Year || (d2Year == d1Year && d2Month > d1Month) || (d2Year == d1Year && d2Month == d1Month && d2Day > d1Day)) {
        return true;
    } else
        return false;
}
function checkDate(type) {
    var startDate = mini.get("txt_estimated_start_time_0").getValue();
    var endDate = mini.get("txt_estimated_end_time_0").getValue();
    if (mini.get("parent_estimated_start_time_")) {
        var parent_estimated_start_time_ = mini.get("parent_estimated_start_time_").getValue();
        var parent_estimated_end_time_ = mini.get("parent_estimated_end_time_").getValue();


        if (CompareDate(startDate, parent_estimated_start_time_)) {
            alert("子任务开始时间不能早于父任务开始时间！");
            mini.get("txt_estimated_start_time_0").setValue(parent_estimated_start_time_);
            return;
        }
        if (CompareDate(parent_estimated_end_time_, endDate)) {
            alert("子任务结束时间不能晚于父任务结束时间！");
            mini.get("txt_estimated_end_time_0").setValue(parent_estimated_end_time_);
            return;
        }
    }
    if (startDate != '' && endDate != '') {
        if (startDate > endDate) {
            if (type == '0') {
                alert('开始时间不能晚于结束时间');
                mini.get("txt_estimated_start_time_0").setValue('');
            } else if (type == '1') {
                alert('结束时间不能早于开始时间');
                mini.get("txt_estimated_end_time_0").setValue('');
            }
        }
    }
}

function checkSearchDate(fromId, toId, type) {
    var timeFrom = mini.get(fromId).getValue();
    var timeTo = mini.get(toId).getValue();
    if (timeFrom != '' && timeTo != '') {
        if (timeFrom > timeTo) {
            if (type == 0) {
                alert('起始时间不能晚于截止时间');
                mini.get(fromId).setValue('');
            } else if (type == 1) {
                alert('截止时间不能早于起始时间');
                mini.get(toId).setValue('');
            }
        }
    }
}
//这个方法将取得 某日期(nowDate) 所在周的星期几(weekDay)的日期
function getNextDate(nowDate, weekDay) {
    //0是星期日,1是星期一,...
    weekDay %= 7;
    var day = nowDate.getDay();
    var time = nowDate.getTime();
    var sub = weekDay - day;
    time += sub * 24 * 3600000;
    nowDate.setTime(time);
    return nowDate;
}
//buttonedit close
function onCloseClick(e) {
    var obj = e.sender;
    obj.setText("");
    obj.setValue("");
}
function GetRequest(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]); return null;
}
function FormatFileSize(filesize) {
    var currFileSize = "";
    if (filesize == null || filesize == "") return "";
    var filesize = parseFloat(filesize.toString());
    if (!isNaN(filesize)) {
        if (filesize < 1024)
            currFileSize = Math.round(filesize * 100) / 100 + "Byte";
        else if (filesize < 1024 * 1024)
            currFileSize = Math.round(filesize / 1024 * 100) / 100 + "KB";
        else if (filesize < 1024 * 1024 * 1024)
            currFileSize = Math.round(filesize / 1024 / 1024 * 100) / 100 + "MB";
        else
            currFileSize = Math.round(filesize / 1024 / 1024 / 1024 * 100) / 100 + "GB";
    }
    return currFileSize;
}

function getFileIcon(fileName) {
    var fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
    fileExtension = fileExtension.toLowerCase();
    var fileIcon = "";  //文件图标路径
    switch (fileExtension) {
        case "doc":
        case "docx":
            fileIcon = "<img src='../Static/img/file-doc.png' border=0 />&nbsp;";
            break;
        case "xls":
        case "xlsx":
            fileIcon = "<img src='../Static/img/file-xls.png' border=0 />&nbsp;";
            break;
        case "ppt":
        case "pptx":
            fileIcon = "<img src='../Static/img/file-ppt.png' border=0 />&nbsp;";
            break;
        case "pdf":
            fileIcon = "<img src='../Static/img/file-pdf.png' border=0 />&nbsp;";
            break;
        case "jpg":
        case "jpeg":
            fileIcon = "<img src='../Static/img/file-jpg.png' border=0 />&nbsp;";
            break;
        case "gif":
        case "bmp":
        case "png":
            fileIcon = "<img src='../Static/img/picture.png' border=0 />&nbsp;";
            break;
        default:
            fileIcon = "<img src='../Static/img/file-null.png' border=0 />&nbsp;";
            break;
    }
    return fileIcon;
}

///验证权限  2014-4-2 eddie
//function ValidationAuthority(patag) {
//    var ret = false;
//    $.ajax({
//        url: "../../AJAX/AjaxService.aspx?method=ValidationHandle",
//        data: { pa_tag: patag },
//        cache: false,
//        success: function (text) {
//            var o = mini.decode(text);
//            ret = o.ret;
//        }
//    });
//    return ret;
//}
 